import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../core/utils/show_toast.dart';
import '../../../../data/controllers/coaches_controller.dart';
import '../../../../data/models/auth/user.dart';

part 'coaches_tab_state.dart';

class CoachesTabCubit extends Cubit<CoachesTabState> {
  CoachesTabCubit() : super(CoachesTabInitial());

  static CoachesTabCubit get(context) => BlocProvider.of(context);

  List<User> users = [];

  void init() {
    getAllUsers();
  }

  Future getAllUsers()async{
    emit(CoachesTabLoading());
    await CoachesController.getMyCoaches().catchError((e){
      emit(CoachesTabError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        users = List<User>.generate(value.data.length, (index) => User.fromJson(value.data[index]));
        emit(CoachesTabDone());
      }else if(value != null){
        showMessage(value.message);
        emit(CoachesTabError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


}
